package com.csair.seam.domain.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csair.seam.domain.mapper.SharingCenterMapper;
import com.csair.seam.domain.mapper.UserBehaviorMapper;
import com.csair.seam.domain.model.entity.EquipCategory;
import com.csair.seam.domain.model.entity.ShareEquip;
import com.csair.seam.domain.model.entity.ShareEquipImage;
import com.csair.seam.domain.model.entity.SysUser;
import com.csair.seam.domain.model.entity.UserBehavior;
import com.csair.seam.domain.model.param.ShareEquipParam;
import com.csair.seam.domain.model.vo.ShareEquipModuleVo;
import com.csair.seam.domain.service.ISharingCenterService;
import com.csair.seam.domain.service.ISysUserService;
import com.csair.seam.infrastructure.utils.DateHelper;

@Service("sharingCenterService")
public class SharingCenterServiceImpl implements ISharingCenterService {

	@Autowired
	private SharingCenterMapper sharingCenterMapper;
	
	@Autowired
	private UserBehaviorMapper userBehaviorMapper;
	
	@Autowired
	private ISysUserService sysUserService;
	

//	@DS("ncc")
	@Override
	public List<ShareEquipModuleVo> findModuleShareEquipList(ShareEquipParam param) {
		List<ShareEquip> shareEquipList = sharingCenterMapper.findModuleSharingEquipList(param);
		return getShareEquipModuleVodList(shareEquipList);
	}
	
	@Override
	public List<ShareEquipModuleVo> findModuleShareEquipListForPc(ShareEquipParam param) {
		List<ShareEquip> shareEquipList = sharingCenterMapper.findSharingEquipByParam(param);
		return getShareEquipModuleVodList(shareEquipList);
	}
	
	private List<ShareEquipModuleVo> getShareEquipModuleVodList(List<ShareEquip> shareEquipList) {
		List<ShareEquipModuleVo> voList = new ArrayList<>();
		for (ShareEquip shareEquip : shareEquipList) {
			ShareEquipModuleVo vo = voList.stream().filter(e->e.getEquipCategory().getCategoryCode().equals(shareEquip.getCategoryCode1())).findFirst().orElse(null);
			if(vo == null) {
				vo = new ShareEquipModuleVo();
				EquipCategory equipCategory = new EquipCategory();
				equipCategory.setId(shareEquip.getCategoryId1());
				equipCategory.setCategoryCode(shareEquip.getCategoryCode1());
				equipCategory.setCategoryName(shareEquip.getCategoryName1());
				vo.setEquipCategory(equipCategory);
				
				List<ShareEquip> list = new ArrayList<>();
				list.add(shareEquip);
				vo.setList(list);
				voList.add(vo);
			}else {
				vo.getList().add(shareEquip);
			}
			
		}
		return voList;
	}
	
//	@DS("ncc")
	@Override
	public List<ShareEquip> findSharingEquipByParam(ShareEquipParam param) {
		return sharingCenterMapper.findSharingEquipByParam(param);
	}
	
	@Override
	public int addOrUpdateUserBehavior(UserBehavior userBehavior) {
		QueryWrapper<UserBehavior> queryWrapper = new QueryWrapper<>(userBehavior);
		UserBehavior updateUserBehavior = userBehaviorMapper.selectOne(queryWrapper);
		String currentDateTimeStr = DateHelper.getCurrentDateTimeStr();
		if(updateUserBehavior != null) {
			updateUserBehavior.setBehaviorTime(currentDateTimeStr);
			return userBehaviorMapper.update(updateUserBehavior, queryWrapper);
		}else {
			userBehavior.setBehaviorTime(currentDateTimeStr);
			return userBehaviorMapper.insert(userBehavior);
		}
	}

	@Override
	public List<UserBehavior> getUserBehaviorList(UserBehavior userBehavior) {
		QueryWrapper<UserBehavior> queryWrapper = new QueryWrapper<>(userBehavior);
		return userBehaviorMapper.selectList(queryWrapper);
	}

	@Override
	public int delUserBehavior(UserBehavior userBehavior) {
		QueryWrapper<UserBehavior> queryWrapper = new QueryWrapper<>(userBehavior);
		return userBehaviorMapper.delete(queryWrapper);
	}
	
	@Override
	public List<ShareEquip> findUserRecommendEquip(String userCode) {
		//1.查询用户近期行为记录
		QueryWrapper<UserBehavior> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(UserBehavior::getUserCode, userCode)
					.orderByDesc(UserBehavior::getBehaviorTime);//按时间倒序
		Page<UserBehavior> page = new Page<UserBehavior>(1, 10);//获取近期前10条记录
		IPage<UserBehavior> selectPage = userBehaviorMapper.selectPage(page, queryWrapper);
		List<UserBehavior> userNearBehaviorList = selectPage.getRecords();//用户近期行为
		
		//2.根据近期行为记录推荐共享设备
		return findRecommendEquipByUserbehavior(userNearBehaviorList);
	}
	
//	@DS("ncc")
	@Override
	public List<ShareEquip> findRecommendEquipByUserbehavior(List<UserBehavior> userNearBehaviorList) {
		if(CollectionUtils.isNotEmpty(userNearBehaviorList)) {//如果有推荐的共享设备
			ShareEquipParam param = new ShareEquipParam();
			List<String> equipCodeList = userNearBehaviorList.stream().map(UserBehavior::getEquipCode).collect(Collectors.toList());
			param.setEquipCodeList(equipCodeList);
			List<ShareEquip> userNearBehaviorEquipList = sharingCenterMapper.findSharingEquipByParam(param);//近期行为记录相关设备
			List<String> categoryIdList = userNearBehaviorEquipList.stream().map(ShareEquip::getCategoryId3).distinct().collect(Collectors.toList());
			param.setEquipCodeList(null);
			param.setCategoryIdList(categoryIdList);
			return sharingCenterMapper.findSharingEquipByParam(param);
		}else {//如果没有推荐的共享设备，推荐热门设备
			return Collections.emptyList();
		}
	}

//	@DS("ncc")
	@Override
	public ShareEquip findShareEquipDetail(String equipCode) {
		ShareEquip shareEquip = sharingCenterMapper.findShareEquipDetail(equipCode);
		if(shareEquip != null) {
			List<ShareEquipImage> shareEquipImageList = sharingCenterMapper.findShareEquipImageList(shareEquip.getId());
			shareEquip.setShareEquipImageList(shareEquipImageList);
		}
		return shareEquip;
	}

//	@DS("ncc")
	@Override
	public List<ShareEquip> findMyOrgShareEquipList(String userCode) {
		SysUser curUser = sysUserService.queryUserByAccount(userCode);
		ShareEquipParam param = new ShareEquipParam();
		param.setOrgId(curUser.getOrgId());
		return sharingCenterMapper.findSharingEquipByParam(param);
	}
	
//	@DS("ncc")
	@Override
	public Page<ShareEquip> findMyOrgShareEquipPage(ShareEquipParam param,String userCode) {
		SysUser curUser = sysUserService.queryUserByAccount(userCode);
		param.setOrgId(curUser.getOrgId());
		Page<ShareEquip> page = new Page<ShareEquip>(param.getPageIndex(), param.getPageSize());
		return sharingCenterMapper.findSharingEquipByParam(page, param);
	}

	@Override
	public Page<ShareEquip> findSharingEquipPage(ShareEquipParam param) {
		Page<ShareEquip> page = new Page<ShareEquip>(param.getPageIndex(), param.getPageSize());
		return sharingCenterMapper.findSharingEquipByParam(page, param);
	}

	@Override
	public ShareEquip findShareEquipThumb(String equipCode) {
		return sharingCenterMapper.findShareEquipThumb(equipCode);
	}

}
